const db = wx.cloud.database()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    meetInfoList: [],
    showList: [],
    inputValue: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    var that = this
    const c = await db.collection('session_info'); //获取集合中记录的总数
    const total = await (await c.count()).total
    const batchTimes = Math.ceil(total / 20)
    console.log(batchTimes) //计算需要获取几次
    let arraypro = [] // 定义空数组 用来存储每一次获取到的记录 

    let x = 0 //这是一个标识每次循环就+1 当x等于batchTimes 说明已经到了最后一次获取数据的时候
    for (let i = 0; i < batchTimes; i++) {
    //分组获取
      await db.collection("session_info").skip(i * 20).get({
        success: function (res) {
          x += 1
          // 20个20个的获取 最后一次不够20 那就是剩下的
          for (let j = 0; j < res.data.length; j++) {
            arraypro.push(res.data[j])
          }
          //判断是否是最后一次，如果是说明已经不用再继续获取了，这时候就可以赋值了
          if (x == batchTimes) {
            var meetInfoList = []
            for (let i in arraypro) {
              var meetInfo = {
                id: arraypro[i]._id,
                movieName: arraypro[i].movieName,
                cinemaName: arraypro[i].cinemaName,
                room: arraypro[i].room,
                date: arraypro[i].date,
                time: arraypro[i].startTime,
                price: '￥' + arraypro[i].price
              }

              meetInfoList.push(meetInfo)
            }

            that.setData({
              meetInfoList: JSON.parse(JSON.stringify(meetInfoList)),
              showList: JSON.parse(JSON.stringify(meetInfoList)),
              inputValue: ''
            })
          }
        }
      })
    }
  },

  search(e) {
    var word = e.detail.value
    var showList = []
    for (const v of this.data.meetInfoList) {
      if (v.movieName.search(word) !== -1) {
        showList.push(JSON.parse(JSON.stringify(v)))
      }
    }

    this.setData({showList: showList})
  },

  cancelSearch() {
    this.setData({
      inputValue: '',
      showList: JSON.parse(JSON.stringify(this.data.meetInfoList))
    })
  },

  createSession() {
    wx.navigateTo({
      url: '../edit_session/edit_session',
    })
  },

  editSessionInfo(e) {
    var index = e.currentTarget.dataset.index
    var id = this.data.showList[index].id
    wx.navigateTo({
      url: `../edit_session/edit_session?id=${id}`,
    })
  },

  async deleteSessionInfo(e) {
    var flag = false
    await wx.showModal({
      title: '提示',
      content: '确认删除吗'
    }).then(res => {
      if (res.confirm) {
        flag = true
      }
    })

    if (!flag) {
      return
    }

    var that = this
    var index = e.currentTarget.dataset.index
    var showList = this.data.showList
    var meetInfoList = this.data.meetInfoList
    await db.collection('session_info').where({
      _id: showList[index].id
    }).remove().then(res => {
      var deleteIdx = 0
      while (deleteIdx < meetInfoList.length) {
        if (meetInfoList[deleteIdx].id === showList[index].id) {
          break
        }

        ++deleteIdx
      }
      showList.splice(index, 1)
      meetInfoList.splice(deleteIdx, 1)
      that.setData({
        showList: showList,
        meetInfoList: meetInfoList
      })
    })

    await wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1000
    })
  }
})